import { formatDate } from '../../utils/format-time';

export interface RowsInterface {
  consumerDate: any;
  amount: any;
  payUserName: any;
  forUserName: any;
  remark: any;
  id: string;
  payPlat: any;
  payWay: any;
  index: any;
  maxPay?: any;
  minPay?: any;
  consumerCount?: any;
}

export const cashBookTableHead: any[] = [
  {
    field: 'index',
    headerName: '序号',
    width: 100,
  },
  { field: 'consumerDate', headerName: '消费日期', width: 180, autoWidth: true, editable: true }, // 自动调整宽度
  {
    field: 'amount',
    headerName: '消费金额',
    width: 180,
    autoWidth: true,
    editable: true,
    renderCell: (params: { value: number }) => {
      const value = Number(params.value) || 0;
      // eslint-disable-next-line no-restricted-globals
      return !isNaN(value) ? value.toFixed(2) : '0.00';
    },
  }, // 自动调整宽度
  { field: 'payUserName', headerName: '支付人', width: 180, autoWidth: true, editable: true }, // 自动调整宽度
  { field: 'forUserName', headerName: '受益人', width: 180, autoWidth: true, editable: true }, // 自动调整宽度
  { field: 'remark', headerName: '备注', width: 180, autoWidth: true, editable: true },
  { field: 'payWay', headerName: '支付方式', width: 180, autoWidth: true, editable: true },
  { field: 'payPlat', headerName: '交易对方', width: 180, autoWidth: true, editable: true },
];

export const getRows = (r: any) =>
  r.data.list.map((row: RowsInterface) => {
    row.consumerDate = formatDate(row.consumerDate);
    // 确保 maxPay 和 minPay 字段存在
    if (row.maxPay === undefined || row.maxPay === null) {
      row.maxPay = row.amount;
    }
    if (row.minPay === undefined || row.minPay === null) {
      row.minPay = row.amount;
    }
    return row;
  });
